﻿$().ready(function(){
	$(".powershell input").keydown(function(e){
		if(e.keyCode == 13){
			var command = $(this).val();
			if(command != ""){
				ps.doCommand($(this).val());
				$(this).val("");
			}
			return false;
		}
		else if(e.keyCode == 38){
			ps.setCommand($(this),"prev");
		}
	}).focus();
});

var ps = ps || {};

$.extend( ps, {
	doCommand : function(command){
		this.logHistory(command);
		this.echoCommand(command);
		dnn.xmlhttp.doCallBack(psControlId, command, this.onSuccess, this, this.onError, null, null, null, 0);
	}

	, logHistory : function(command){
		var his = this.history || (this.history = []);
		for(var i = 0; i < this.history.length; i++){
			if(this.history[i] == command){
				this.history.splice(i, 1);
				break;
			}
		}
		his.unshift(command);
		this.hitHistory = 0;
	}

	, setCommand : function(target, type){
		if(this.history != undefined){
			target.val(this.history[this.hitHistory]);
			this.utils.setCaretTo(target.get(0), target.val().length - 1);
			if(type == "prev"){
				this.hitHistory = this.hitHistory == this.history.length - 1 ? 0 : this.hitHistory + 1;
			}
			else{
				this.hitHistory = this.hitHistory == 0 ? this.history.length - 1 : this.hitHistory - 1;
			}
		}
	}

	, onSuccess : function(result, context){
		if(result == "")
			context.echoBack("No message return from server.", "#666");
		else
			context.echoBack(result);
	}

	, onError : function(result, context){
		context.echo("Command Executed Failed: " + result, "#f00");
	}

	, echo : function(msg, color){
		var win = this.echoWin || (this.echoWin = $(".powershell .echo"));
		msg = this.format(msg);
		if(color != undefined){
			msg = "<span style=\"color: " + color + "\">" + msg + "</span>";
		}
		win.html(win.html() + msg);
		win.scrollTop(win[0].scrollHeight);
	}

	, format : function(source){
		source = source.replace(/\r\n/g, "<br />");
		source = source.replace(/    /g, "&nbsp;&nbsp;&nbsp;&nbsp;");
		source = source.replace(/\t/g, "&nbsp;&nbsp;&nbsp;&nbsp;");

		if(source.substr(source.length - 6, 6) != "<br />"){
			source += "<br />";
		}
		return source;
	}

	, echoCommand : function(command){
		this.echo("> " + command + "<br />");
	}

	, echoBack : function(msg, color){
		color = color || '#0f0';
		this.echo(msg, color);
	}
});

ps.utils = {
  setCaretTo : function(element, start, end) {
    if(element.createTextRange) {
      var range = element.createTextRange();
      range.moveStart('character', start);
      range.moveEnd('character',   (end || start));
      range.select();
    } else if(element.selectionStart) {
      element.focus();
      element.setSelectionRange(start, (end || start));
    }
  }
  
  , getCaretPosition : function(element) {
    if (typeof element.selectionStart == 'number'){
      return element.selectionStart;
    } else if (document.selection) {
      var range = document.selection.createRange();
      var rangeLength = range.text.length;
      range.moveStart('character', -element.value.length);
      return range.text.length - rangeLength;
    }
  }
}